from django.utils.decorators import method_decorator

from gis.admin.decorators import require_login, check_permission
from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from itpay.core.agency import agency_service
from itpay.server_admin.decorators import require_vcode
from itpay.server_admin.view.agency.helper import wrapper_agency_info
from .agency_forms import *
from itpay.core.sys import export_service, const as export_const
from .helper import wrapper_name


class AgencyView(BaseView):
    @method_decorator(require_login)
    # @method_decorator(check_permission('agency_get'))
    @method_decorator(validate_parameters(AgencyListSchema))
    def get(self, request, cleaned_data):
        if cleaned_data.get('level'):
            cleaned_data['score'] = agency_service.calculate_score_by_level(cleaned_data['level'])
            del cleaned_data['level']
        total, agency = agency_service.list_agency(return_many_to_many=True, **cleaned_data)
        return PaginationResponse(total,
                                  [wrapper_agency_info(e, wrap_level=True, wrap_avatar=True, wrap_min_user_level=True)
                                   for e in agency])

    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('agency_admin_add'))
    @method_decorator(validate_parameters(AddAgencySchema))
    def post(self, request, cleaned_data):
        return agency_service.create_agency(**cleaned_data, agency_type=AgencyType.ADMIN_AGENCY)


class AgencyDetailView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('agency_get'))
    def get(self, request, agency_id):
        return wrapper_agency_info(agency_service.get_agency_by_id(agency_id, with_mch_info=True))

    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('agency_update'))
    @method_decorator(validate_parameters(UpdateAgencySchema))
    def put(self, request, agency_id, cleaned_data):
        agency_service.update_agency(agency_id, **cleaned_data)
        return dict(id=agency_id)


class ComplaintView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('complain_get'))
    @method_decorator(validate_parameters(ComplainAgencySchema))
    def get(self, request, cleaned_data):
        if cleaned_data.get('export'):
            del cleaned_data['export']
            task = export_service.create_export_task(cleaned_data, export_const.ExportType.USER_COMPLAIN)
            return dict(task_id=task['id'])
        total, complain = agency_service.list_complaint_agency(**cleaned_data)
        return PaginationResponse(total, [wrapper_name(e) for e in complain])


class ComplaintDetailView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('complain_get'))
    def get(self, request, complain_id):
        complain = agency_service.get_complain_by_id(complain_id)
        return complain


class HandleComplaintView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('complain_handle'))
    @method_decorator(validate_parameters(HandleComplainSchema))
    def put(self, request, complain_id, cleaned_data):
        agency_service.handle_complain(complain_id=complain_id, remark=cleaned_data['remark'])
        return dict(id=complain_id)


class ResetPasswordAgency(BaseView):
    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('agency_resetpwd'))
    def put(self, request, agency_id):
        return agency_service.reset_password(agency_id)
